import {
  Body,
  Controller,
  Get,
  Post,
  Query,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express/multer/interceptors/file.interceptor';
import { ApiBearerAuth, ApiBody, ApiConsumes, ApiOperation, ApiTags } from '@nestjs/swagger';
import { FileServiceImpl } from '../service/impl/fileServiceImpl';
import { AnyFilesInterceptor } from '@nestjs/platform-express/multer/interceptors/any-files.interceptor';
import {
  FileListDto,
  FileInsertDto,
  FileDeleteDto,
} from '@/api/file/dto/file.dto';
import { FileEntity } from '@/entity/file.entity';
import { pageRecords } from '@/interface/pageInterface';

@ApiBearerAuth()
@ApiTags('文件管理')
@Controller('common/file')
export class FileController {
  constructor(private readonly FileServiceImpl: FileServiceImpl) {}

  // 单文件
  @Post('/upload')
  @ApiOperation({ description: '上传单个文件' })
  @ApiBody({
    description: '图片,视频文件',
    type: FileInsertDto,
  })
  @ApiConsumes('multipart/form-data')
  @UseInterceptors(FileInterceptor('file'))
  async uploadFile(@UploadedFile() file): Promise<unknown> {
    // console.log('file', file);
    return this.FileServiceImpl.uploadToQiniu(file);
  }

  /**
   *注意，这里的多文件是：在上传按钮弹出选择后，可以选择多个文件，而不是同时上传多个name不同的file
   */
  //  多个文件
  /*  @ApiBody({
    description: '图片,视频文件',
    type: FileUrlCreateDto,
  })
  @ApiOperation({ description: '上传多个文件' })
  @ApiConsumes('multipart/form-data')
  @Post('/uploads')
  @UseInterceptors(AnyFilesInterceptor())
  async uploadFiles(@UploadedFiles() files): Promise<unknown> {
    console.log('files', files);
    return this.FileServiceImpl.uploadToQiniu(files);
  }
 */
  @Get('/list')
  @UseInterceptors(AnyFilesInterceptor())
  async list(@Query() query: FileListDto): Promise<pageRecords<FileEntity>> {
    return this.FileServiceImpl.list(query);
  }
  @Post('/delete')
  async delete(@Body() body: FileDeleteDto): Promise<unknown> {
    const { fileURL } = body;
    return this.FileServiceImpl.delete(fileURL);
  }
}
